package com.ruoyi.web.controller.warning;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.warning.domain.entity.Links;
import com.ruoyi.warning.domain.query.LinkQuery;
import com.ruoyi.warning.mapper.LinksMapper;
import com.ruoyi.warning.service.ILinksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-04-07
 */
@RestController
@RequestMapping("/links")
@Api(tags = "links", description = "用于处理链接和网络拓扑相关操作的控制器")
public class LinksController {
    @Resource
    private ILinksService linksService;
    @Resource
    private LinksMapper linksMapper;

    @ApiOperation("获取相关网络拓扑")
    @GetMapping(value = "/getNetworkTopology/{id}")
    public R getNetworkTopologyById(@PathVariable String id)
    {
        return R.ok(linksService.getNetworkTopologyById(id));
    }

    @ApiOperation("获取全部网络拓扑")
    @GetMapping(value = "/getNetworkTopology")
    public R getNetworkTopology()
    {
        return R.ok(linksService.getNetworkTopology());
    }

    @ApiOperation("新增或修改网络连接")
    @PostMapping(value = "/addNetworkTopology")
    public R addNetworkTopology(@RequestBody Links links)
    {
        if("0".equals(links.getManual())){
            linksService.saveOrUpdate(links);
            return R.ok();
        }
        if(!ObjectUtils.isEmpty(links.getId())&&"1".equals(links.getManual())){
            linksMapper.removeLink(links.getId().toString());
        }
        linksMapper.addLink(links);
        return R.ok();
    }

    @ApiOperation("删除网络连接")
    @DeleteMapping(value = "/deleteNetworkTopology")
    public R deleteNetworkTopology(@RequestBody Links links)
    {
        if("1".equals(links.getManual())){
            linksMapper.removeLink(links.getId().toString());
        }else{
            linksService.removeById(links.getId());
        }
        return R.ok();
    }

    @ApiOperation("分页获取内网关键链路")
    @GetMapping(value = "/getCriticalLink")
    public R getCriticalLink(LinkQuery query)
    {
        return R.ok(linksService.getCriticalLink(query));
    }

    @ApiOperation("关键链路详情")
    @GetMapping(value = "/detail/{id}")
    public R detail(@PathVariable String id)
    {
        Links links = linksMapper.getById(id);
        return R.ok(links);
    }

    @ApiOperation("新增或修改关键链路")
    @PostMapping(value = "/addOrUpdateCriticalLink")
    public R addOrUpdateCriticalLink(@RequestBody Links links)
    {
        linksService.addOrUpdateCriticalLink(links);
        return R.ok();
    }

}
